jQuery.fn.exists = function(){return this.length>0;}

function test(){
	alert('test');
}

function submitForm(targetForm){
	//call preValidate function defined in individual jsp
	if(typeof preValidate == 'function'){
		if(preValidate() == false){
			return false;
		}
	}
	
	if(targetForm.form('validate') == false){
       return false;
    }
	else{
		targetForm.submit();
	}
}

function validateAndSubmitForm(targetForm){
	//call preValidate function defined in individual jsp
	if(typeof preValidate == 'function'){
		if(preValidate() == false){
			return false;
		}
	}
	
	var validateUrl = targetForm.attr('validateUrl');
	var json = JSON.stringify(targetForm.serializeJSON());
	//alert(validateUrl+"&&&&"+json);
	$.ajax( {
		type : 'POST',
		contentType : 'application/json',
		url : validateUrl,
		dataType : 'json',
		data : json,
		beforeSend : function(xhr, opts){
	        if(targetForm.form('validate') == false){
	            xhr.abort();
	        }
	    },
		success : function(returnVal) {
			//alert(returnVal.result);
			if (returnVal.result != 'success'){ 
				clearError(targetForm);
				showError(returnVal.errField, returnVal.errMsg)
			} else {  
				targetForm.submit();
			}
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
		  //alert(JSON.stringify(XMLHttpRequest)+"||"+textStatus+"||"+errorThrown);
		  alert(XMLHttpRequest.status+"||"+XMLHttpRequest.readyState+"||"+textStatus);
			//$.messager.alert('消息', '修改失败','info');
		}
	});
}

function showError(errField, errMsg){
	var errSpan = "<p><span class='ui-icon ui-icon-alert' style='float: left'></span><strong>";
	errSpan += errMsg + "</strong></p>";
	$(errSpan).appendTo("#errMsgDiv");
	$("#errMsgDiv").css('display', '');
	
	$("#"+errField).addClass("ui-state-error").focus();
	/*
	for ( var i = 0; i < errMsgs.length; i++) {
		var errSpan = "<p><span class='ui-icon ui-icon-alert' style='float: left'></span><strong>";
		errSpan += errMsgs[i] + "</strong></p>";
		$(errSpan).appendTo("#errMsgDiv");
	}
	if(errMsgs.length > 0){
		$("#errMsgDiv").css('display', '');
	}
	*/
};

function clearError(targetForm){
	var formId = targetForm.attr("id");
	//$("#loginFailDiv").css('display', 'none');
	$("#errMsgDiv").html("");
	$("#"+formId+" :input").removeClass("ui-state-error");
}

function clearForm(targetForm){
	targetForm.form('clear');
}

function formatSuppInd(val,row){
    if(val == "0"){
    	return "N";
    }
    else{
    	return "Y";
    }
}

$(function() {
	//return:13,esc:27, f1-f12: 112-123
	var keyCodeArr = [27, 112, 113, 114,115,116,117,118,119,120,121,122,123];
	//disable all hotkey
	$(function() {
		document.onkeydown = function (event) {
			switch (event.keyCode){
				case 112 :
				document.onhelp = function() {return(false);}
				window.onhelp = function() {return(false);}
			}
			
			if(keyCodeArr.indexOf(event.keyCode) > -1){
				event.stopPropagation();
		        event.preventDefault();
		        //alert(event.keyCode);
		        event.keyCode = 0;
		        return false;
			}
			
		};
		
	});
});
